'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
  //event为客户端上传的参数
	const fs = require("fs");
	let status=0;
	let user;
	const collection = db.collection('users');
	const users = await collection.where({"user_name": event.user_name}).get();
	if(users.data.length==0){
		status=2;
		// let icon=event.icon;
		// if(icon.length==0){
		// 	icon="https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg";
		// }else{
		// 	 let up_img = await uniCloud.uploadFile({
		// 		 cloudPath: "icons/"+event.user_name+",jpg",
		// 		 fileContent: new Buffer(img, 'base64')
		// 	 });			
		// }
		user={
			"user_name": event.user_name,
			"password": event.password,
			"icon":event.icon,
			"sex":event.sex,
			"characters":[],
			"focused_users":[],
			"is_focused_users":[],
		};
	let res=await collection.add(user);
	}else{
		user=users.data[0];
	}

  //返回数据给客户端
	return {"status":status,"user":user}
};
